$(function () {
    //变量抽取
    var applicant_datagrid = $("#applicant_datagrid");
    var applicantitem_datagrid = $("#applicantitem_datagrid");
    var applicant_dialog = $("#applicant_dialog");
    var applicant_form = $("#applicant_form");
    var recruiteditId = null;
    applicant_datagrid.datagrid({
        url: '/applicant/list',
        toolbar: "#toolbar",
        striped: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        pagePosition: "bottom",
        fitColumns: true,
        fit: true,
        columns: [[
            {
                field: "recruitDetail",
                title: '详单id',
                width: 100,
                align: 'center',
                hidden: true,
                formatter: function (value, row, index) {
                    if (value) {
                        return value.id;
                    } else {
                        return '';
                    }
                }
            },
            {
                field: "name",
                title: '姓名',
                width: 100,
                align: 'center',
            },
            {
                field: "sex",
                title: '性别',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return '';
                    }
                }
            },
            {
                field: "education",
                title: '教育程度',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return '';
                    }
                }
            },
            {
                field: "worktime", title: '经验(/年)', width: 100, align: 'center'
            },
            {
                field: "workexperience",
                title: '经历',
                width: 100,
                align: 'center',
            },
            {
                field: 'states', title: '审批状态', width: 100, align: 'center',
                formatter: function (value) {
                    switch (value) {
                        case 0:
                            return "<span style='color: #fff513'>待审</span>";
                        case 1:

                            return "<span style='color: #2347ff'>录取</span>";
                        case 2:
                            return "<span style='color: red'>已被干</span>";
                        default:
                    }

                }
            }
        ]]
    });


    var methodObj = {
        //刷新
        reload: function reload() {
            applicant_datagrid.datagrid("load");
        },

        //取消
        cancel: function cancel() {
            applicant_dialog.dialog("close");
        },

        //查看招聘详单
        queryItem: function queryItem() {

            var row = applicant_datagrid.datagrid('getSelected');
            //recruiteditId赋值
            recruiteditId = row.recruitDetail.id;
           // alert(recruiteditId);
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }


            applicantitem_datagrid.datagrid({
                striped: true,
                fitColumns: true,
                fit: true,
                url: "/recruitDetail/queryById?id=" + recruiteditId,
                columns: [[

                    {
                        field: 'id', title: '编号', width: 100, sortable: true, hidden: true,
                    },

                    {field: 'job', width: 100, align: 'center', title: '招聘岗位', editor: 'text'},

                    {
                        field: 'recruit',
                        title: '制定人id',
                        width: 100,
                        align: 'center',
                        hidden: true,
                        formatter: function (value) {
                            return value ? value.id : '';
                        }
                    },

                    {
                        field: 'detail_recruitType', width: 100, align: 'center', title: '招聘方式',
                        formatter: function (value, row) {
                            if (row.recruitType.name) {
                                return row.recruitType.name;
                            }
                            return '';
                        }
                    },
                    {
                        field: 'engage', width: 100, align: 'center', title: '聘用形式',
                        formatter: function (value, row) {
                            if (value) {
                                return value.name;
                            }
                            return '';
                        }
                    },

                    {field: 'recruitNumber', width: 100, align: 'center', title: '所需人数', editor: 'text'},

                    {field: 'completedNumber', width: 100, align: 'center', title: '已录用人数', editor: 'text'},
                    {
                        field: 'education', width: 100, align: 'center', title: '学历要求',
                        formatter: function (value, row) {
                            if (value) {
                                return value.name;
                            }
                            return '';
                        }
                    },
                    {field: 'skillRequired', width: 100, align: 'center', title: '技能要求', editor: 'text'},
                    {
                        field: 'recruitSalary', width: 100, align: 'center', title: '薪资情况',
                        formatter: function (value, row) {
                            if (value) {
                                return value.name;
                            }
                            return '';
                        }
                    }

                    ]],
            });

            applicant_dialog.dialog("setTitle", '招聘计划详情信息');
            applicant_dialog.dialog("open");


        },


        audit: function () {
            var row = applicant_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }

            if(row.states!=0){
                $.messager.alert('温馨提示', "已禁止修改!", 'warning');
                return;
            }

            layui.use('layer', function () {
                var layer = layui.layer;
                layer.open({
                    content: "确定要正式录取"+"[" + row.name  + "]"+"为我司员工么吗?"
                    , btn: ['录用', '干掉', '取消']
                    , yes: function (index, layero) {
                        //录取了
                        $.ajax({
                            url: '/applicant/admission?id='+row.id,
                           // data: {id: row.id},
                          //  dataType: 'json',
                            type: 'get',
                            success: function (data) {
                                if (data.success) {
                                    layer.open({
                                        content: "初步信息已保存到员工系统,请及时完善"+"[" + row.name  + "]"+"的个人信息",
                                        btn: ['确认'],
                                        yes: function (index, layero) {
                                           $("#applicant_datagrid").datagrid("reload");
                                            layer.close(index);
                                        }
                                    });
                                } else {
                                    layer.open({
                                        content: data.msg,
                                        btn: ['确认'],
                                        yes: function (index, layero) {
                                            $("#applicant_datagrid").datagrid("reload");
                                            layer.close(index);
                                        }
                                    });
                                }
                            }
                        });

                    }
                    , btn2: function (index, layero) {
                        //被干掉了
                        $.ajax({
                            url: '/applicant/disAdmission?id='+row.id,
                         //   data: {id: row.id},
                          //  dataType: 'json',
                            type: 'get',
                            success: function (data) {
                                if (data.success) {
                                    layer.open({
                                        content: "操作成功",
                                        btn: ['确认'],
                                        yes: function (index, layero) {
                                            $("#applicant_datagrid").datagrid("reload");
                                            layer.close(index);
                                        }
                                    });
                                } else {
                                    layer.open({
                                        content: data.msg,
                                        btn: ['确认'],
                                        yes: function (index, layero) {
                                            $("#applicant_datagrid").datagrid("reload");
                                            layer.close(index);
                                        }
                                    });
                                }
                            }
                        });
                    },
                    btn3: function (index, layero) {
                        layer.close(index);
                    }
                });
            });
        }
    };

    //调用方法
    $('a[data-cmd]').click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    applicant_dialog.dialog({
        width: 900,
        height: 320,
        modal: true,
        closed: true,
        onClose: function () {
            applicant_dialog.form("clear");
        }
    });
});
























